package TCP02;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @author 宋鑫宇
 * @version 1.0
 */
class ServiceReaderThread implements Runnable {
    private Socket socket;

    public ServiceReaderThread() {
    }

    public ServiceReaderThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //3.从Socket管道中得到一个字节输入流
            InputStream inputStream = socket.getInputStream();
            //4.把字节输入流包装成字符输入流来接收文本消息
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            //5.读取消息
            String msg;
            //循环接收到消息，如果没有消息后会报错
            while ((msg = br.readLine()) != null) {
                System.out.println(socket.getRemoteSocketAddress() + "发来了：" + msg);
            }
        } catch (IOException e) {
            System.out.println(socket.getRemoteSocketAddress() + "下线了");
        }
    }
}
